﻿@model Sealight.Core.DataAdapter.user

@{
    ViewBag.Title = "Cập nhật tài khoản";
    Layout = "~/Areas/Admin/Views/Shared/__AdminLayout.cshtml";

    string id = string.Empty, userName = string.Empty, emailAddress = string.Empty, password = string.Empty;
    
    if(Model != null)
    {
        id = Model.id.ToString();
        userName = Model.user_name;
        emailAddress = Model.email_address;
        password = Model.password;
    }
}
@section _Head{
    <link rel="Stylesheet" type="text/css" href="@Url.Content("~/Content/Plugins/jqueryPasswordValidator/jquery.validate.password.css")" />
    <script type="text/javascript" src="@Url.Content("~/Content/Plugins/jqueryPasswordValidator/jquery.validate.password.js")"></script>
    <script type="text/javascript">
        $(document).ready(function () {         

            $("#btn_save_profile").button();

            $("#btn_save_profile").click(function () {
                $("#account_form").trigger("submit");
                return false;
            });

            $("#password_field").change(function () {
                $('#isPasswordChange').val('true');
                $("#retype_password_field").val('');

            });


            var container = $('div.container_error');

            //FORM VALIDATE & SUBMIT              
            //form validate & submit
            $("#account_form").validate({
                rules: {
                    email_field: {// compound rule 
                        required: true,
                        email: true

                    },
                    password_field: {
                        rangelength: [6, 15]

                    },
                    retype_password_field: {
                        equalTo: "#password_field"
                    }
                },
                messages: {
                    email_field: {
                        required: "Email bắt buộc nhập.",
                        email: "Sai địa chỉ email."
                    },
                    retype_password_field: { equalTo: "Mật khẩu không phù hợp" }
                },
                errorElement: "li",
                errorContainer: container,
                errorLabelContainer: $(".error", container)
            ,
                submitHandler: function (form) {
                    var userId = $('#user_id').val();
                    var username = $('#user_name').val();
                    var email = $('#email_field').val();
                    var psswd = $('#oldPassword').val(); //$('#password_field').val();
                    if ($('#isPasswordChange').val() == 'true') {
                        psswd = $('#password_field').val();
                    }
                    JsUtils.ShowLoader('#edit_profile_container', 'Lưu...');
                    $.ajax({
                        type: "POST",
                        url: "/admin/users/UpdateProfile",
                        data: ({ email: email, password: psswd, userId: userId, username: username }),
                        dataType: "json",
                        success: function (d) {
                            if (d != null) {

                                if (d.OperationComplete != null && d.OperationComplete == false && d.ErrorCode != null) {
                                    if (d.ErrorFriendlyMessage != null) {
                                        ShowErrorMessage(d.ErrorFriendlyMessage);
                                    }
                                    return;
                                }
                                else {
                                    ShowErrorMessage("Lưu thành công", "thông báo");
                                }
                            }
                        },
                        complete: function (d) {
                            JsUtils.hideLoader('#edit_profile_container');
                        },
                        cache: false
                    }); //end .ajax call
                }

            }); //end form.validate       
        });

        function LoadProfile() {

            $.ajax({
                type: "POST",
                url: "/users/GetProfile",
                data: ({}),
                dataType: "json",
                success: function (d) {
                    if (d != null) {

                        if (d.OperationComplete != null && d.OperationComplete == false && d.ErrorCode != null) {
                            if (d.ErrorFriendlyMessage != null) {
                                ShowErrorMessage(d.ErrorFriendlyMessage);
                            }
                            return;
                        }
                        if (d.id != null) {
                            $('#user_id').val(d.id);
                        }

                        if (d.user_name != null) {
                            $("#username_display").html(d.user_name);
                            $('#user_name').val(d.user_name);
                        }

                        if (d.password != null) {
                            //Load temp password
                            $("#password_field").val("P@ssword123$");
                            $("#retype_password_field").val("P@ssword123$");
                            //$("#password_field").val(d.password);
                            //$("#retype_password_field").val(d.password);
                            $('#oldPassword').val(d.password);
                        }

                        if (d.email_address != null) {
                            $("#email_field").val(d.email_address);
                        }
                    }
                },
                cache: false
            });          //end .ajax call
        }

</script>
}

<div id="edit_profile_container" style="overflow: auto; width: 100%; height: 95%;
    margin-top: 55px;">
    <form id="account_form" method="post" action="" enctype="multipart/form-data">
    <fieldset style="width: 600px;" class="Internal">
        <legend class="Internal">Tài khoản đăng nhập</legend>
        <div class="container_error">
            <h5>
                Vui lòng kiểm tra lại các lỗi...</h5>
            <ol class="error">
            </ol>
        </div>
        <div id="infoColumn" style="width: 400px; float: left;">
            <table class="Internal">
                <tbody>
                    <tr>
                        <td style="padding-top: 10px;">
                            <strong class="Internal">Tên đăng nhập:</strong>
                        </td>
                        <td>
                            <span class="Internal" id="username_display" >@userName</span>
                        </td>
                    </tr>
                </tbody>
            </table>
            <table class="Internal">
                <tbody>
                    <tr>
                        <td style="width: 350px;">
                            <strong class="Internal">*Email</strong>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type="text" id="email_field" name="email_field" style="width: 340px" class="Internal"  value="@emailAddress"/>
                        </td>
                    </tr>
                </tbody>
            </table>
            <table class="Internal">
                <tbody>
                    <tr>
                        <td style="padding-top: 10px;">
                            <strong class="Internal">*Mật khẩu</strong>
                        </td>
                        <td>
                            <div class="password-meter">
                                <div class="password-meter-message" style="height: 15px;">
                                </div>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 150px;">
                            <input type="password" id="password_field" name="password_field" style="width: 145px" value="P@ssword123$"
                                class="Internal password" />
                        </td>
                        <td>
                            <div class="password-meter">
                                <div class="password-meter-bg">
                                    <div class="password-meter-bar">
                                    </div>
                                </div>
                            </div>
                            <small style="color: #a7a7a7;">password strength</small>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" style="padding-top: 10px;">
                            <strong class="Internal">*Nhập lại mật khẩu</strong>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <input type="password" id="retype_password_field" name="retype_password_field" value="P@ssword123$" style="width: 145px"
                                class="Internal" />
                            <br />
                        </td>
                        <td>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <br clear="all" />
        <br />
        <div style="width: 500px; text-align: right; margin-bottom: 10px; float: left;">
            <input type="hidden" name="isPasswordChange" id="isPasswordChange" value="false" />
            <input type="hidden" name="oldPassword" id="oldPassword" value="@password" />
            <input type="hidden" id="user_name" value="@userName"/>
            <input type="hidden" id="user_id" value="@id"/>
            <a href="javascript:void(0);" id="btn_save_profile">Lưu </a>
        </div>
    </fieldset>
    </form>
</div>
